<!-- 商城用户地址编辑 -->
<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人:</text>
			<input class="input" type="text" v-model="addressData.real_name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">电话:</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">所在地区:</text>
			<text @click="openAddress" class="input">
				{{addressName}}
			</text>
			<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor="#FF1F2F"></simple-address>
		</view>
		<view class="row addressDel b-b">
			<text class="tit">详细地址:</text>
			<textarea class="textarea" auto-height="true" v-model="addressData.detail" placeholder="写到门牌、楼层房间号等信息"></textarea>
			<!-- <input class="input" v-model="addressData.detail" placeholder="选择地址"></input> -->
			<!-- <text @click="chooseLocation" class="yticon icon-shouhuodizhi"></text> -->
		</view>
		<!-- <view class="row b-b">
			<text class="tit">邮编</text>
			<input class="input" type="number" v-model="addressData.post_code" placeholder="邮政编码" placeholder-class="placeholder" />
		</view> -->
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.is_default ? true : false" color="#FF1F2F" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
	</view>
</template>

<script>
	import { addressEdit } from '@/api/mall.js'
	import simpleAddress from '@/components/simple-address/simple-address.nvue'
	export default {
		data() {
			return {
				addressData: {
					real_name: '',
					phone: '',
					address: '',
					is_default: 0,
					detail: ''
				},
				addressName: '选择区域',
				cityPickerValueDefault: [0,0,0]
			}
		},
		components: {
			simpleAddress
		},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
				this.addressName = this.addressData.province+'-'+this.addressData.city+'-'+this.addressData.district;
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			switchChange(e){
				this.addressData.is_default = e.detail.value;
			},
			openAddress(){
				this.$refs.simpleAddress.open();
			},
			onConfirm(e){
				this.pickerText = JSON.stringify(e);
				this.addressName = e.label;
				let arrAdd = e.label.split('-'); 
				this.addressData.province = arrAdd[0];
				this.addressData.city = arrAdd[1];
				this.addressData.district = arrAdd[2];
			},
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						this.addressData.detailInfo = data.name;
						this.addressData.latitude = data.latitude;
						this.addressData.longitude = data.longitude;
					}
				})
			},
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.real_name){
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(data.phone)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if(!data.province){
					this.$api.msg('选择所在地区');
					return;
				}
				if(!data.detail){
					this.$api.msg('请填写详细地址');
					return;
				}
				let postdata = {
					address: {
						province: this.addressData.province,
						city: this.addressData.city,
						district: this.addressData.district,
						
					},
					is_default: this.addressData.is_default ? 1 : 0,
					detail: this.addressData.detail,
					real_name: this.addressData.real_name,
					// post_code: this.addressData.post_code,
					phone: this.addressData.phone,
					id: this.addressData.id || 0,
				}
				addressEdit(postdata).then(res => {
					this.$api.msg('添加成功');
					if(res.status==200) return uni.navigateBack();
				})
				// this.$api.request('address/saveOrUpdate','POST',postdata,true,true).then(res=>{
				// 	if(res.code==0) return uni.navigateBack();
				// })
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #f8f8f8;
		padding-top: 16rpx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30rpx;
		height: 110rpx;
		background: #fff;
		border-bottom: 1rpx #eee solid;
		
		.tit{
			flex-shrink: 0;
			width: 160rpx;
			font-size: 30rpx;
			color: #303133;
		}
		.input{
			flex: 1;
			font-size: 30rpx;
			color: #303133;
		}
		.textarea{
			min-height: 120upx;
			color: #303133;
		}
		
		.icon-shouhuodizhi{
			font-size: 48rpx;
			color: $mall-price;
		}
	}
	.addressDel{
		padding: 30rpx;
		align-items: flex-start;
	}
	.default-row{
		border: none;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16rpx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 100rpx auto 0;
		font-size: 32rpx;
		color: #fff;
		background-color: $mall-price;
		border-radius: 40rpx;
		width: 690rpx;
	}
</style>
